<template>
  <div class="panel-header">
    <div class="panel-title">
      <i class="panel-icon realtime-icon"></i>
      <span>实时产能统计</span>
    </div>
    <div class="panel-controls">
      <el-select v-model="selectedEquipment" placeholder="选择设备" clearable size="small" @change="onEquipmentChange">
        <el-option
          v-for="item in equipmentList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select v-model="timeWindow" placeholder="时间窗口" size="small" @change="onTimeWindowChange">
        <el-option label="15分钟" :value="15" />
        <el-option label="30分钟" :value="30" />
        <el-option label="60分钟" :value="60" />
      </el-select>
    </div>
  </div>
  <div class="panel-body">
    <div ref="chartRef" class="chart-container"></div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  equipmentList: {
    type: Array,
    default: () => []
  },
  initialSelectedEquipment: {
    type: String,
    default: ''
  },
  initialTimeWindow: {
    type: Number,
    default: 15
  }
})

const emit = defineEmits(['update:selectedEquipment', 'update:timeWindow'])

const chartRef = ref(null)
const chart = ref(null)
const selectedEquipment = ref(props.initialSelectedEquipment)
const timeWindow = ref(props.initialTimeWindow)

// 监听设备选择变化
watch(() => selectedEquipment.value, (newValue) => {
  emit('update:selectedEquipment', newValue)
  updateChart()
})

// 监听时间窗口变化
watch(() => timeWindow.value, (newValue) => {
  emit('update:timeWindow', newValue)
  updateChart()
})

// 监听设备列表变化
watch(() => props.equipmentList, () => {
  updateChart()
}, { deep: true })

// 初始化图表
const initChart = () => {
  if (!chartRef.value) return

  chart.value = echarts.init(chartRef.value)

  // 设置图表响应式
  const resizeHandler = () => {
    chart.value && chart.value.resize()
  }
  window.addEventListener('resize', resizeHandler)

  // 组件卸载时移除事件监听
  onBeforeUnmount(() => {
    window.removeEventListener('resize', resizeHandler)
    chart.value && chart.value.dispose()
  })

  updateChart()
}

// 更新图表数据
const updateChart = () => {
  if (!chart.value) return

  // 生成模拟数据
  const now = new Date()
  const times = []
  const productionData = []
  const speedData = []

  // 根据时间窗口生成数据点
  const interval = timeWindow.value === 60 ? 5 : (timeWindow.value === 30 ? 2 : 1)
  const points = timeWindow.value / interval

  for (let i = 0; i < points; i++) {
    const time = new Date(now - (points - i) * interval * 60 * 1000)
    times.push(time.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }))

    // 随机生成产量和速度数据
    productionData.push(Math.round(Math.random() * 100 + 50))
    speedData.push(Math.round(Math.random() * 20 + 10))
  }

  // 模拟数据
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985'
        }
      }
    },
    legend: {
      data: ['产量', '速度']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: times,
        axisLabel: {
          rotate: 45
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        name: '产量(米)',
        position: 'left'
      },
      {
        type: 'value',
        name: '速度(米/分钟)',
        position: 'right'
      }
    ],
    series: [
      {
        name: '产量',
        type: 'line',
        smooth: true,
        areaStyle: {
          opacity: 0.3
        },
        emphasis: {
          focus: 'series'
        },
        data: productionData,
        itemStyle: {
          color: '#67C23A'
        }
      },
      {
        name: '速度',
        type: 'line',
        smooth: true,
        yAxisIndex: 1,
        emphasis: {
          focus: 'series'
        },
        data: speedData,
        itemStyle: {
          color: '#409EFF'
        }
      }
    ]
  }

  chart.value.setOption(option)
}

// 处理设备选择变化
const onEquipmentChange = (value) => {
  selectedEquipment.value = value
}

// 处理时间窗口变化
const onTimeWindowChange = (value) => {
  timeWindow.value = value
}

onMounted(() => {
  initChart()
})
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 100%;
}
</style>
